'use strict';
const requireEx=require('yi-require');

const middleware = function(req,res,next){
    let doc = req.body;

    let authInfo =  req.context||req.context.authInfo ;
    if(!req.context || !req.context.authInfo){
        return res.err.unauthorized();
    }
    authInfo = authInfo.authInfo;

    let userId = ObjectID(authInfo._id);
    if(doc.defaultBool){
        req.mongo.collection("address").update({userId:userId},{$set:{defaultBool:false}},(e,r)=>{
            if(e) return res.err.mongoError(e);
            next();
        });
    }else{
        next();
    }
};

module.exports = {
    open:false,
    description:'更新地址',
    middleware:middleware,
    handler: requireEx('yi-restful').u('address')
};